using UnityEngine;
using Delta2DGame.Base;
using Delta2DGame.State;

/// <summary>
/// 角色状态机控制器
/// </summary>
public class PlayerStateMachine : MonoBehaviour
{
    public PlayerState currentState { get; private set; }
    [SerializeField] private PlayerController player;

    // 状态实例
    public IdleState idleState { get; private set; }
    public RunState runState { get; private set; }
    public JumpState jumpState { get; private set; }

    private void Awake()
    {
        // 自动获取PlayerController组件
        if (player == null)
            player = GetComponent<PlayerController>();

        // 初始化所有状态
        idleState = new IdleState(this, player, "isidel");
        runState = new RunState(this, player, "isrun");
        jumpState = new JumpState(this, player, "isjump");
    }

    private void Start()
    {
        // 默认进入待机状态
        SwitchState(idleState);
    }

    private void Update()
    {
        currentState?.Update();
    }

    private void FixedUpdate()
    {
        currentState?.FixedUpdate();
    }

    /// <summary>
    /// 切换状态
    /// </summary>
    public void SwitchState(PlayerState newState)
    {
        currentState?.Exit();
        currentState = newState;
        currentState.Enter();
    }
}